# Create your views here.
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
import time
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from feedbackcn.fb.models import Message

def feedback(request):
    if request.method == 'POST':
	ISOTIMEFORMAT='%Y-%m-%d'
	m = Message(	username = request.POST['username'],
			email	 = request.POST['email'],
			telephone = "13426",
			public    = "1",
			title     = request.POST['subject'],
			content   = request.POST['message'],
			datetime  =time.strftime( ISOTIMEFORMAT, time.localtime( time.time() ) ),
			companyid = "123",
			ip        ="192.168.0.1"
			)
	m.save()
    res = Message.objects.filter(companyid='123').order_by('-id').all()
    paginator = Paginator(res, 10)
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        list = paginator.page(page)
    except (EmptyPage, InvalidPage):
        list = paginator.page(paginator.num_pages)
 
    responseContext = {'list':list,
                        }
    resp = render_to_response('fb/index.html', responseContext,
                                context_instance=RequestContext(request))
    return resp

def home(request):
    
    responseContext = {'test':"test",
                        }
    resp = render_to_response('fb/home.html', responseContext,
                                context_instance=RequestContext(request))
    return resp

def price(request):
    
    responseContext = {'test':"test",
                        }
    resp = render_to_response('fb/price.html', responseContext,
                                context_instance=RequestContext(request))
    return resp

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.http import HttpResponseRedirect


def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/feedback")
    else:
        form = UserCreationForm()
    return render_to_response("registration/register.html", {
        'form': form,
    })

